﻿Imports System.IO
Imports System.Text
Imports System.Web
Imports MeoTom.Core.DAL

Namespace BLL
    Public Class Tags
        Private _TagID As Integer
        Private _TagName As String
        Private _TotalTag As Integer
        Private _ArticleID As Integer

        Public tPrv As TagProvider
        Private util As New Utilities.Common()

        Public Property ArticleID() As Integer
            Get
                Return _ArticleID
            End Get
            Set(ByVal value As Integer)
                _ArticleID = value
            End Set
        End Property

        Public Property TagID() As Integer
            Get
                Return _TagID
            End Get
            Set(ByVal value As Integer)
                _TagID = value
            End Set
        End Property

        Public Property TagName() As String
            Get
                Return _TagName
            End Get
            Set(ByVal value As String)
                _TagName = value
            End Set
        End Property

        Public Property TotalTag() As Integer
            Get
                Return _TotalTag
            End Get
            Set(ByVal value As Integer)
                _TotalTag = value
            End Set
        End Property

        Public Sub New()
            tPrv = New TagProvider()
        End Sub

        Public Sub Create()
            tPrv.Create(Me)
        End Sub

        Public Function Update() As Boolean
            Dim b As Boolean = False
            Try
                Dim reader As SqlClient.SqlDataReader = tPrv.Update(Me)
                If reader.Read() Then
                    If reader("TagUpdateStatus").ToString() = "1" Then
                        b = False
                    Else
                        b = True
                    End If
                End If
                reader.Close()
            Catch ex As Exception
                b = False
            End Try
            Return b
        End Function

        Public Sub Delete()
            tPrv.Delete(Me)
        End Sub

        Public Sub GetByID()
            Try
                Dim reader As SqlClient.SqlDataReader = tPrv.GetByID(Me)
                If reader.Read Then
                    Me.TagID = Integer.Parse(reader("TagID").ToString())
                    Me.TagName = reader("TagName").ToString()
                    Me.TotalTag = Integer.Parse(reader("TotalTag").ToString())
                End If
                reader.Close()
            Catch ex As Exception
            End Try
        End Sub

        Public Function GetAll() As DataSet
            Return tPrv.GetAll()
        End Function

        Public Function GetTagByArticleEdit() As String
            Dim strReturn As String = ""
            Try
                Dim i As Integer = 0
                Dim strCom As String = " , "
                Dim reader As SqlClient.SqlDataReader = tPrv.GetTagByArticleEdit(Me)
                While reader.Read
                    If i = 0 Then
                        strReturn &= reader("TagName").ToString
                    Else
                        strReturn &= strCom & reader("TagName").ToString
                    End If
                    i = i + 1
                End While
                reader.Close()
            Catch ex As Exception
            End Try
            Return strReturn
        End Function

        Public Function GetTag() As String
            Dim strReturn As String = ""
            Try
                Dim i As Integer = 0
                Dim strCom As String = ","
                Dim reader As SqlClient.SqlDataReader = tPrv.GetTagByArticleEdit(Me)
                While reader.Read
                    If i = 0 Then
                        strReturn &= "<a href=""/t" & reader("TagID").ToString & "/" & util.utf8_to_ascii(reader("TagName").ToString) & """ title=""" & reader("TagName").ToString & """>" & reader("TagName").ToString & "</a>"
                    Else
                        strReturn &= strCom & "<a href=""/t" & reader("TagID").ToString & "/" & util.utf8_to_ascii(reader("TagName").ToString) & """ title=""" & reader("TagName").ToString & """>" & reader("TagName").ToString & "</a>"
                    End If
                    i = i + 1
                End While
                reader.Close()

                Me.GetDefaultTag(0)
            Catch ex As Exception
            End Try
            Return strReturn
        End Function

        Public Function GetTagByArticleUpdate(ByVal TargetID As String) As String
            Dim strReturn As String = ""
            Try
                Dim i As Integer = 0
                Dim strCom As String = " , "
                Dim reader As SqlClient.SqlDataReader = tPrv.GetAllTags()
                While reader.Read
                    If i = 0 Then
                        strReturn &= "<a href=""javascript:Tags.AddThisTag('" & reader("TagName").ToString & "','" & TargetID & "');"" title=""Add this tag"">" & reader("TagName").ToString & "</a>"
                    Else
                        strReturn &= strCom & "<a href=""javascript:Tags.AddThisTag('" & reader("TagName").ToString & "','" & TargetID & "');"" title=""Add this tag"">" & reader("TagName").ToString & "</a>"
                    End If
                    i = i + 1
                End While
                reader.Close()
            Catch ex As Exception
            End Try
            Return strReturn
        End Function

        Public Function ReadDefaultTag() As String
            Dim strReturn As String = ""
            Try
                'Dim stream As New System.IO.FileStream(Configs.FolderSaveData & "0afa541d68697b0b25e9b740ee9dab80_Tags.htm", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.ReadWrite)
                Dim reader As New StreamReader(Configs.FolderSaveData & "0afa541d68697b0b25e9b740ee9dab80_Tags.htm", System.Text.Encoding.UTF8)
                strReturn = reader.ReadToEnd()
                reader.Close()
            Catch ex As Exception
            End Try
            Return strReturn
        End Function

        Public Function GetArticleByTag() As DataSet
            Return tPrv.GetArticleByTag(Me)
        End Function

        Public Function GetCloud(ByVal tagData As DataTable, ByVal cloudTemplate As String) As String
            Dim outputBuffer As New StringBuilder()
            Dim max As Double
            Dim min As Double

            Double.TryParse(tagData.Compute("min(TotalTag)", Nothing).ToString(), min)
            Double.TryParse(tagData.Compute("max(TotalTag)", Nothing).ToString(), max)

            For Each row As DataRow In tagData.Rows
                Dim weightPercent As Double = (Double.Parse(row("TotalTag").ToString()) / max) * 100
                Dim weight As Integer = 0

                If weightPercent >= 99 And max > 1 Then
                    weight = 5
                ElseIf weightPercent >= 70 And max > 1 Then
                    weight = 4
                ElseIf weightPercent >= 40 And max > 1 Then
                    weight = 3
                ElseIf weightPercent >= 20 And max > 1 Then
                    weight = 2
                ElseIf weightPercent >= 3 Then
                    weight = 1
                Else
                    weight = 0
                End If

                If weight > 0 Then
                    outputBuffer.Append(cloudTemplate.Replace("$weight$", weight.ToString()).Replace("$tag$", row("TagName").ToString()).Replace("$urlencodetag$", util.utf8_to_ascii(row("TagName").ToString())).Replace("$urlencodetagid$", row("TagID").ToString()))
                End If
            Next
            Return outputBuffer.ToString()
        End Function

        Public Function GetDefaultTag(ByVal type As Integer) As String
            Dim strReturn As New StringBuilder()
            Dim strCloudTemp As String = ""
            Try
                If type = 0 Then
                    strReturn.Append("<div class=""otherBox fl""><h3>Tags</h3><div class=""hr fl"">&nbsp;</div><div class=""content-box""><ul id=""tagCloud"">")

                    strCloudTemp = "<li><a rel=""nofollow"" href=""/t$urlencodetagid$/$urlencodetag$"" class=""weight$weight$"" title=""$tag$"">$tag$</a></li>"

                    strReturn.Append(GetCloud(tPrv.GetTagData().Tables(0), strCloudTemp))

                    strReturn.Append("</ul>")
                    strReturn.Append("</div></div>")

                    Dim writer As New StreamWriter(Configs.FolderSaveData & "0afa541d68697b0b25e9b740ee9dab80_Tags.htm", False, System.Text.Encoding.UTF8)
                    writer.Write(strReturn)
                    writer.Flush()
                    writer.Close()
                ElseIf type = 1 Then
                    strCloudTemp = "<a href=""TagUpdate.aspx?action=Edit&TagID=$urlencodetagid$&TagName=$urlencodetag$"" class=""weight$weight$"" title=""$tag$"">$tag$</a>"
                    strReturn.Append(GetCloud(tPrv.GetTagData().Tables(0), strCloudTemp))
                End If
            Catch ex As Exception
            End Try
            Return strReturn.ToString()
        End Function

        Public Sub UpdateTotalArticle()
            Try
                Dim reader As SqlClient.SqlDataReader = tPrv.GetTagByArticle(Me)
                While reader.Read()
                    If Integer.Parse(reader("TotalTag").ToString()) > 0 Then
                        Me.TagID = Integer.Parse(reader("TagID").ToString())
                        Me.TotalTag = Integer.Parse(reader("TotalTag").ToString()) - 1
                        tPrv.UpdateTotalArticle(Me)
                    End If
                End While
                reader.Close()
            Catch ex As Exception
            End Try
        End Sub
    End Class
End Namespace
